package gameStates.gui;

import gameStates.passes.AbsPassManagerState;
import interfaces.GUISource;

import main.listeners.listenerClasses.SettingsListener;
import main.listeners.listenerClasses.loading.LoadingAbortedEvent;
import main.listeners.listenerClasses.loading.LoadingListener;
import main.listeners.listenerClasses.loading.LoadingStartedEvent;
import main.listeners.listenerClasses.loading.LoadingStoppedEvent;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.experimental.FPSLabel;
import org.fenggui.util.Color;

import settings.CommonSettings;

public class FPSState extends GUIState {
		
	private FPSLabel fpsLabel;
	
	public FPSState(AbsPassManagerState passManagerState) {
		super("fps", passManagerState, true);
		
		addListenerToClearer(new SettingsListener() {
			@Override
			public void commonSettingsChanged() {
				setActive(CommonSettings.get().getDrawFPS());
			}
			
			@Override
			public void graphicSettingsChanged() {}
			@Override
			public void controlSettingsChanged() {}
			@Override
			public void soundSettingsChanged() {}
		});
		
		addListenerToClearer(new LoadingListener() {
			@Override
			public void loadingStopped(LoadingStoppedEvent event) {
				putAtEnd();
			}
			
			@Override
			public void loadingStarted(LoadingStartedEvent event) {
				putAtEnd();
			}
			
			@Override
			public void loadingAborted(LoadingAbortedEvent event) {
				putAtEnd();
			}
		});
		
		initInterface();
	}
	
	public void initInterface() {
		ImageFont font = GUISource.middleFont;
		int width = font.getWidth("9999 fps");
		int height = font.getHeight() / 3 * 4;
		
		fpsLabel = new FPSLabel();
		GUISource.setTheme(fpsLabel);
		GUISource.setFont(fpsLabel, GUISource.middleFont);
		GUISource.setTextColor(fpsLabel, Color.WHITE);
		fpsLabel.setSize(width, height);
		fpsLabel.setXY(guiDisp.getWidth() - width, guiDisp.getHeight() - height);
		
		guiDisp.addWidget(fpsLabel);
	}
	
	@Override
	public void restore() {
		super.restore();
		initInterface();
	}
}